package com.mindao.app.ued.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mindao.app.common.base.controller.BaseCoreController;
import com.mindao.app.common.spring.service.util.JsonResult;
import com.mindao.app.ued.model.Tpltb1;
 

@Controller
@RequestMapping("/ued/tpltb1")
public class Tpltb1Controller extends BaseCoreController {
	private static Logger log = LoggerFactory.getLogger(Tpltb1Controller.class);
	private static String hsql_all = "from Tpltb1";
	@RequestMapping
	public String menu_list(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs) throws Exception {
		
		this.getPageData(hsql_all,rhs);
		rhs.addAttribute("info_type", "success");
		rhs.addAttribute("info", "");
		return  super.rtnDefault(request, response, session, rhs);
	}
	@RequestMapping
	public String create(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs) throws Exception {
		Tpltb1 tpltb1 = new Tpltb1();
		tpltb1.setName("");
		baseService.create(tpltb1);
		getPageData(hsql_all,rhs);
		List countList = baseService.find(hsql_all);
		int maxPage = countList.size() % pagination.getMaxSize() > 0 ? countList
				.size() / pagination.getMaxSize() + 1
				: countList.size() / pagination.getMaxSize();
		pagination.setCurrentPage(maxPage);
		List tpltb1List = baseService.page("from Tpltb1", pagination);
		rhs.addAttribute("dataList", tpltb1List);
		rhs.addAttribute("maxSize", pagination.getMaxSize());
		rhs.addAttribute("count", countList.size());
		rhs.addAttribute("maxPage", maxPage);
		rhs.addAttribute("currentPage", maxPage);
		rhs.addAttribute("info_type", "success");
		rhs.addAttribute("info", "create success!");
		return  super.rtnDefault(request, response, session, rhs);
	}
	@RequestMapping
	public String change_rank(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs) throws Exception {
		common_change_rank(); 
		rhs.addAttribute("info_type", "success");
		rhs.addAttribute("info", "改变顺序成功!");
		getPageData(hsql_all,rhs);
		return  super.rtnDefault(request, response, session, rhs);
	}
	@RequestMapping( method = RequestMethod.POST)
	public String delete(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs,@RequestParam (required = false) String id) throws Exception {
		//String id = getpara("id");
		Tpltb1 tpltb1data = new Tpltb1();
		tpltb1data.setId(Long.parseLong(id));
		baseService.delete(tpltb1data);
		getPageData(hsql_all,rhs);
		rhs.addAttribute("info_type", "success");
		rhs.addAttribute("info", "delete success!");
		return  super.rtnDefault(request, response, session, rhs);
	}
	
	@RequestMapping

	public String update(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs) throws Exception {
		common_update(hsql_all,rhs);
		return  super.rtnDefault(request, response, session, rhs);
	}   
	
     //修改每页显示的个数
	@RequestMapping
	@ResponseBody
	public JsonResult change_page_number(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs) throws Exception {
		putSessionValue("maxSize", getpara("maxSize"));
		getPageData(hsql_all,rhs);
		JsonResult json=new JsonResult(JsonResult.SUCCESS,"success!");
		return json;
	}
     //翻页
	@RequestMapping
	@ResponseBody
	public JsonResult ajax_page_data(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs) throws Exception {
		getPageData(hsql_all,rhs);
 
		JsonResult json=new JsonResult(JsonResult.SUCCESS,"success!");
		return json;
	}

}
